/*global location: false, YAHOO: false, jQuery: false, window: false, Results: false, RECORDS_PER_PAGE: false, ResultsColumnDefs: false, emptyMessage: false, location_search: true */

/*members ac_param, add, append, apply_to, bv_options, counter, each, 
    filter_options, freebaseSuggest, get_filter_options, html, indexOf, 
    parent, q, remove, reset, rm, siblings, type, type_for_freebase_suggest, 
    val
*/

/* begin: Filter object */
var Filter = {

    q: {},
    counter: 0,

    /* Add filter field */
    add: function () {
        jQuery('#filters_div').append('<div class="filter_block">' + 'and' + '&nbsp;<select class="filter_type" id="filter_type' + this.counter + '">' + jQuery('select.first').html() + '</select>' + '&nbsp;<input type="text" size="10" class="filter_query" id="filter_query' + this.counter + '" />' + '&nbsp;<input type="button" class="filter_add" value="+" />' + '&nbsp;<input type="button" class="filter_rm" value="-" style="margin-right: 1.8em;" />' + '</div>');

        jQuery('input.filter_query').each(function () {
            var s = jQuery(this).siblings('select.filter_type'), v = s.val();            
            bv_options.ac_param.type = type_for_freebase_suggest(v);            
            jQuery(this).freebaseSuggest(bv_options);
        });

        this.counter += 1;
    },

    /* Remove filter field */
    rm: function (el) {
        jQuery(el).parent().remove();
    },

    /* Getter of filter options hash */
    get_filter_options: function () {
        location_search = '';
        var t = {}, k;
        // Save columns added by user
        for (k in filter_options) {
            if ((k.indexOf('col_') !== - 1) || (k.indexOf('sort_by_') !== - 1)) {
                t[k] = filter_options[k];
            }
        }
        jQuery('select.filter_type').each(function (i) {
            var v = jQuery(this).siblings('input.filter_query').val() || "";
            t[jQuery(this).val()] = v;
        });
        this.q = t;
        return this.q;
    },

    /* Apply filters to table */
    apply_to: function (table) {
        table.reset();
    }
};
